package com.jcark.ui.widget.datepicker

import android.app.Activity
import android.app.Dialog
import android.graphics.Color
import android.os.Build
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.contrarywind.view.WheelView
import com.jcark.ui.R
import java.util.Calendar

/**
 * Description:日期时间选择器工具类 <br>
 * @author King.Zi<br>
 * @date 2025/03/12<br>
 * Email: 78688689@qq.com
 */
object DatePickerUtils {
    /**
     * 创建日历选择器
     */
    fun createDateTimeDialog(
        activity: Activity,
        dateTimeParameter: DateTimeParameter,
        timeSelectListener: OnTimeSelectListener,
        confirmListener: View.OnClickListener,
        cancelListener: View.OnClickListener?,
    ): MTimePickerView {
        fun getString(strResId: Int): String {
            return activity.resources.getString(strResId)
        }

        val initDate = Calendar.getInstance()
        if (!TextUtils.isEmpty(dateTimeParameter.initDate)) {
            val dateArray = dateTimeParameter.initDate.split("-".toRegex())
            initDate.set(dateArray[0].toInt(), dateArray[1].toInt() - 1, dateArray[2].toInt())
        }

        val startDate = Calendar.getInstance()
        if (!TextUtils.isEmpty(dateTimeParameter.startDate)) {
            val dateArray = dateTimeParameter.startDate.split("-".toRegex())
            startDate.set(dateArray[0].toInt(), dateArray[1].toInt() - 1, dateArray[2].toInt())
        }

        val endDate = Calendar.getInstance()
        if (!TextUtils.isEmpty(dateTimeParameter.endDate)) {
            val dateArray = dateTimeParameter.endDate.split("-".toRegex())
            endDate.set(dateArray[0].toInt(), dateArray[1].toInt() - 1, dateArray[2].toInt())
        }

        var timePickerView: MTimePickerView? = null
        //时间选择器
        val timePickerBuilder = MTimePickerBuilder(activity, timeSelectListener)
            .setLayoutRes(R.layout.dialog_date_picker_view) { rootView: View ->
                rootView.findViewById<TextView>(R.id.tvCancel).setOnClickListener { view ->
                    timePickerView?.dismiss()
                    cancelListener?.onClick(view)
                }
                rootView.findViewById<TextView>(R.id.tvConfirm).setOnClickListener { v ->
                    timePickerView?.returnData()
                    confirmListener.onClick(v)
                    timePickerView?.dismiss()
                }
            }
            .setType(dateTimeParameter.type)
            .setContentTextSize(15)
            .setDate(initDate)
            .setRangDate(startDate, endDate)
            .isDialog(true, dateTimeParameter.isBottomDialog)
            .setDividerType(WheelView.DividerType.FILL)
            .setLineSpacingMultiplier(2f) //每行item间距
            .setOutSideColor(Color.parseColor("#B3000000")) //弹窗以外背景色
            .setOutSideCancelable(true)
            .isCenterLabel(dateTimeParameter.isCenterLabel)
            .setItemVisibleCount(5)//最多显示条数
            .setDecorView(activity.window.decorView.findViewById(android.R.id.content)) //处理遮挡虚拟按键

        if (dateTimeParameter.isShowLabel) {
            timePickerBuilder.setLabel(
                getString(R.string.pickerview_year),
                getString(R.string.pickerview_month),
                getString(R.string.pickerview_day),
                getString(R.string.pickerview_hours),
                getString(R.string.pickerview_minutes),
                getString(R.string.pickerview_seconds)
            )
        } else {
            timePickerBuilder.setLabel("", "", "", "", "", "")
        }

        timePickerView = timePickerBuilder.build()
        //系统返回键监听屏蔽掉
        timePickerView.setKeyBackCancelable(false)
        return timePickerView
    }

    /**
     * 设置dialog沉浸式
     */
    fun setDialogImmersive(dialog: Dialog) {
        dialog.window?.apply {
            // 获取视口全屏大小
            decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            //这个flag表示window负责绘制状态栏的背景当设置了这个flag,系统状态栏会变透明,同时这个相应的区域会被填满 getStatusBarColor() and getNavigationBarColor()的颜色
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)

            statusBarColor = Color.TRANSPARENT
            val layoutParams = attributes
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
            navigationBarColor = Color.TRANSPARENT
            //内容扩展到导航栏
            setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL)
            if (Build.VERSION.SDK_INT >= 28) {
                layoutParams.layoutInDisplayCutoutMode =
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            }
            attributes = layoutParams
        }
    }
}